package com.feilong.modules.controller.order;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feilong.modules.common.exception.APIException;
import com.feilong.modules.model.order.constant.OrderInfoConstant;
import com.feilong.modules.model.order.request.AddOrderRequest;
import com.feilong.modules.model.order.request.OrderInfoRequest;
import com.feilong.modules.model.order.request.UpdateOrderRequest;
import com.feilong.modules.model.order.vo.OrderDetailVO;
import com.feilong.modules.model.order.vo.OrderInfoVO;
import com.feilong.modules.service.order.OrderInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping("order/info")
public class OrderInfoController {

    @Resource
    OrderInfoService orderInfoService;

    /**
     * 从云开发服务器获取订单列表信息
     * @param query
     * @return
     */
    @PostMapping("list")
    public Page<OrderInfoVO> getOrderInfo(@RequestBody OrderInfoRequest query){
        if (Objects.isNull(query)) {
            throw new APIException(OrderInfoConstant.ORDER_INFO_ERROR);
        }
        return orderInfoService.getOrderInfo(query);
    }

    /**
     * 根据订单编号获取云开发服务器订单详情
     * @param orderId
     * @return
     */
    @GetMapping("getOrderDetail/{orderNumber}")
    public OrderDetailVO getOrderDetail(@PathVariable("orderNumber") String orderId){
        if (StringUtils.isBlank(orderId)) {
            throw new APIException(OrderInfoConstant.ORDER_DETAIL_UNEXIST);
        }
        return orderInfoService.getOrderDetail(orderId);
    }

    /**
     * 新增订单信息
     * @param orderInfo
     */
    @PostMapping("addOrderInfo")
    public void addOrderInfo(@RequestBody @Valid AddOrderRequest orderInfo){
        if (CollectionUtils.isEmpty(orderInfo.getOrderDetail())) {
            throw new APIException(OrderInfoConstant.ORDER_INFO_ERROR);
        }
        orderInfoService.addOrderInfo(orderInfo);
    }

    /**
     * 删除云开发数据库订单信息
     * @param orderID
     */
    @GetMapping("delOrder/{orderID}")
    public void delOrder(@PathVariable("orderID") String orderID){
        if (StringUtils.isBlank(orderID)) {
            throw new APIException(OrderInfoConstant.ORDER_NUMBER_UNEXIST);
        }
        orderInfoService.delOrder(orderID);
    }

    /**
     * 更新云开发数据库订单信息
     * @param updateOrderRequest
     */
    @PostMapping("updateOrder")
    public void updateOrder(@RequestBody @Valid UpdateOrderRequest updateOrderRequest){
        orderInfoService.updateOrder(updateOrderRequest);
    }

}
